#pragma GCC optimize(2)
#pragma GCC optimize(3)

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 1e5 + 3;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int A[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int n, t;
char a[N], ans[N];

int can(int x, int y) {
    if (x * 2 <= y && x * 7 >= y)return 1;
    return 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    for (cin >> t; t; t--) {
        cin >> n >> a + 1;
        int tot = 0;
        for (int i = 1; i <= n; i++)tot += A[a[i] - '0'];
        for (int i = 1; i <= n; i++) {
            for (int j = 9; j >= 0; j--) {
                if (can(n - i, tot - A[j])) {
                    ans[i] = (j + '0');
                    tot -= A[j];
                    break;
                }
            }
        }
        ans[n + 1] = '\0';
        cout << ans + 1 << '\n';
    }
    return 0;
}
